package com.wyq.jpa.dao;


import com.wyq.jpa.model.Blog;
import com.wyq.jpa.model.Permission;
import com.wyq.jpa.model.Role;
import com.wyq.jpa.model.User;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* Loads sample data for the sample app since it's an in-memory database.
*/
@Component
public class BootstrapDataPopulator implements InitializingBean {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserDao userDao;
    @Autowired
    private BlogDao blogDao;

//    public void setDataSource(DataSource dataSource) {
//        this.dataSource = dataSource;
//    }

    public void afterPropertiesSet() throws Exception {
        //because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
        //app starts, so insert the sample admin user at startup:
//        JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
//        jdbcTemplate.execute("insert into roles values (1, 'user', 'The default role given to all users.')");
//        jdbcTemplate.execute("insert into roles values (2, 'admin', 'The administrator role only given to site admins')");
//        jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')");
//        jdbcTemplate.execute("insert into users(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + new Sha256Hash("admin").toHex() + "')");
//        jdbcTemplate.execute("insert into users_roles values (1, 2)");
        if(blogDao.findAll().size()<1){
            Blog blog=new Blog();
            blog.setBackendCommentSize(5);
            blog.setBackendPageSize(4);
            blog.setCommentAudit(1);
            blog.setDescription("完美世界");
            blog.setEntryNumber(5);
            blog.setName("完美世界");
            blog.setRecentCommentNumber(4);
            blog.setRecentEntryNumber(5);
            blog.setStatus(1);
            blogDao.save(blog);

            User user=new User();
            user.setDescription("完美世界");
            user.setEmail("towangyanqing@gmail.com");
            user.setLoginName("admin");
            user.setName("admin");
            user.setOriPassword("admin");
            user.setPassword(new Sha256Hash("admin").toHex());
            user.setRegisterDate(new Date());

            Set<Permission> permissions=new HashSet<Permission>();
            Permission permission=new Permission();
            permission.setPermissionName("user:*");
            permission.setPermissionName("用户管理");
            permissions.add(permission);

            Set<Role> roles=new HashSet<Role>();
            Role role=new Role();
            role.setName("管理员");
            role.setDescription("管理员");
            role.setPermissions(permissions);
            roles.add(role);
            user.setRoles(roles);

            userDao.save(user);
        }
    }
}
